package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Resultarticle;
import com.rk.admin.service.ResultService;
import com.rk.admin.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "科学研究/学术动态")
@RestController
@CrossOrigin //前后端分离配置跨域访问
@RequestMapping("/result")
public class ResultarticleController {


    @Autowired
    private ResultService resultService;


    @ApiOperation(("分页查询科学研究文章"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始页码",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页数据条数",required = true,example = "5"),
            @ApiImplicitParam(name = "title",value = "文章标题",example = "榆林市"),
    })
    @GetMapping("/findPage")
    public CommonResult findPageBen(Integer page,Integer limit, String title){
        return CommonResult.success(resultService.selectPage(page,limit,title));
    }


//    /**
//     * 添加文章
//     * @param resultarticle
//     * @return
//     */
//    @ApiOperation("添加文章")
//    @SystemControllerLog(description ="添加项目成果文章")
//    @PostMapping("/add")
//    public CommonResult add(@RequestBody Resultarticle resultarticle) {
//        Boolean flag = resultService.add(resultarticle);
//        if(flag){
//            return CommonResult.success(flag);
//        }else {
//            return CommonResult.failed("添加失败!!");
//        }
//    }


    /**
     * 通过id删除
     * @param id
     * @return
     */
    @ApiOperation("删除文章")
    @SystemControllerLog(description ="删除科学研究文章")
    @PostMapping("/deletebyid")
    public CommonResult deletebyid(@RequestParam("id")Integer id){
        Boolean flag = resultService.deleteById(id);
        if(flag){//删除成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("删除失败!");
        }
    }


    /**
     * 通过id查询文章信息
     */
    @ApiOperation("通过id查询文章")
    @GetMapping("/findbyid")
    public CommonResult findbyid(@RequestParam("id") Integer id){
        List<Resultarticle> list=new ArrayList<>();
        list.add(resultService.findbyId(id));
        return  CommonResult.success(list);
    }


    /**
     * 修改文章
     */
    @ApiOperation("通过id修改文章")
    @SystemControllerLog(description ="修改科学研究文章")
    @PostMapping("/updatebyid")
    public CommonResult updatebyid(@RequestBody Resultarticle resultarticle){
        System.out.println();
        //设置修改时间
        resultarticle.setEdittime(new Date());
        Boolean flag = resultService.updatebyId(resultarticle);

        if(flag){//如果修改成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("修改失败!");
        }
    }

}
